﻿#region namespaces

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

#endregion

namespace SLIITPro.PasswordRecover
{
    public partial class reseturl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSubmit.Click += new EventHandler(btnSubmit_Click);
            if (Page.IsPostBack) return;
            if (Request.QueryString["p"] != null)
                if (Request.QueryString["p"].Length == 16)
                    GetValideRequest(Request.QueryString["p"]);
                else
                {
                    MvwChange.ActiveViewIndex = -1;
                    headsearch.InnerText = "your password request not valide";
                }
            else MvwChange.ActiveViewIndex = 0;
        }

        private UserExtension _userExtension;
        private void GetValideRequest(string reqId)
        {
            _userExtension = new UserExtension();
            DataRow row;
            if (_userExtension.IsValideRequest(reqId, out row))
            {
                var dt = DateTime.Parse(row[2].ToString()).AddHours(24);
                var count = DateTime.Compare(dt, DateTime.Now);
                if (count > 0)
                {
                    MvwChange.ActiveViewIndex = 1;
                    Session.Add("RAuthUser", row[1].ToString());
                }
                else
                {
                    MvwChange.ActiveViewIndex = -1;
                    headsearch.InnerText = "your password request has been expaired try again..";
                }
            }
            else
            {
                MvwChange.ActiveViewIndex = -1;
                headsearch.InnerText = "your password request not valide";
            }
        }


        protected void btnSubmitConfirmation_Click(object sender, EventArgs e)
        {
            GetValideRequest(txtConfirmationCode.Text.Trim());
        }

        private Users _users;
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _userExtension = new UserExtension();
            var userdata = _userExtension.SelectUser("UserId", Session["RAuthUser"].ToString());
            _users = new Users(userdata.Rows[0]) { Password = txtNewPass.Text };
            _userExtension.UpdateUser(_users);
            Session.Remove("RAuthUser");
            Response.Redirect(Core.CoreSettings.RelativeWebRoot);
        }
    }
}
